from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from framework.base_page import BasePage
from model.advance_search_criteria import AdvanceSearchCriteria
from model.advance_search_result import AdvanceSearchResult


'''高级搜索页面'''

class AdvanceSearchPage(BasePage):
    def __init__(self, driver: WebDriver):
        super(AdvanceSearchPage, self).__init__(driver)
        self.wait(By.CSS_SELECTOR, '.advanced-filters')

    def search(self, search_criteria: AdvanceSearchCriteria, limit=0) -> list[AdvanceSearchResult]:
        if search_criteria.keyword:
            # 输入搜索
            self.find(By.CSS_SELECTOR, ".search-bar>input").send_keys(search_criteria.keyword)
        if search_criteria.category:
            # 选择分类
            self.click(By.ID, "search-in-category")
            self.find(By.CSS_SELECTOR, '#search-in-category input[name=filter-input-search]'). \
                send_keys(search_criteria.category)
            self.click(By.CSS_SELECTOR, '#search-in-category li[title='" + search_criteria.category + "']')
        if search_criteria.topic:
            # 选择话题
            self.click(By.ID, "search-status-options")
            self.click(By.CSS_SELECTOR, "#search-status-options li[title='" + search_criteria.topic + "']")
        if search_criteria.tags:
            # 选择拥有标签
            self.click(By.ID, "search-with-tags")
            for tag in search_criteria.tags:
                self.find(By.CSS_SELECTOR, '#search-with-tags input[name=filter-input-search]').\
                    send_keys(tag)
                self.click(By.CSS_SELECTOR, "li[title='" + tag + "']")
                self.find(By.CSS_SELECTOR, '#search-with-tags input[name=filter-input-search]').clear()
            self.click(By.ID, "search-with-tags")
        # 点击搜索
        self.click(By.CSS_SELECTOR, "button[aria-label='搜索'].btn-primary")
        self._wait_loading()
        # 获取搜索结果
        return self._get_search_result(limit)

    def _wait_loading(self):
        self.wait_disappear(By.CSS_SELECTOR, '.spinner')

    def _get_search_result(self, limit=0) -> list[AdvanceSearchResult]:
        res_list = []
        res_eles = self.finds(By.CSS_SELECTOR, ".search-results .fps-result-entries div[role='listitem']")
        num = 0
        for res_ele in res_eles:
            # 限制返回数量，小于等于0时，返回当前页所有数据
            if 0 < limit <= num:
                break
            search_result = AdvanceSearchResult()
            # 获取标题
            search_result.title = res_ele.find_element(By.CSS_SELECTOR, ".topic-title .ember-view").text
            # 获取内容
            search_result.content = \
                res_ele.find_element(By.CSS_SELECTOR, ".fps-topic>.blurb.container>.ember-view").text
            # 获取分类
            categorys = res_ele.find_elements(By.CSS_SELECTOR, ".fps-topic .search-category .category-name")
            search_result.categorys = [category.text for category in categorys]
            # 获取标签
            tags = res_ele.find_elements(By.CSS_SELECTOR, ".fps-topic .discourse-tags a")
            search_result.tags = [tag.text for tag in tags]

            res_list.append(search_result)
            num += 1
        return res_list



